//
//  ProductDetail_Vm.swift
//  Pods
//
//  Created by eme on 2017/3/28.
//
//

import RxSwift
import NetWorkCore
import SwiftyJSON
import UtilCore
import AddCarCore
/*
 owner:cy
 update:2016年12月17日12:02:55
 info: 首页数据
 VC:   ProductDetail_Vc
 */
class ProductDetail_Vm :Base_Vm {
    
    /// 表示正在刷新中
    let loading = ActivityIndicator()
    
    /*
     接收返回的数据
     */
    /// 返回首页数据
    let productItem = PublishSubject<ProductModel>()
    
    let addcartsSuccess = PublishSubject<JSON>()
    
    
    let addcartsSubject = PublishSubject<NSString>()
    
    let createOrderSubject = PublishSubject<[String: AnyObject]>()
    let createOrderSuccess = PublishSubject<JSON>()
    
    ///添加收藏按钮
    let addusPdCollectionSubject = PublishSubject<(userid:String,pdid:String)>()
    /// 删除商品
    let delusPdCollectionSubject = PublishSubject<String>()
    //如果是添加收藏则 返回cid 否则返回""
    let pdCollectionSuccess =  PublishSubject<String>()
    /*
     通用设置
     */
    init(refreshTriger: Observable<String>) {
        super.init()
        let refreshSubject = Observable.of(
            refreshTriger,
            reloadTrigger.withLatestFrom(refreshTriger))
            .merge()
        
        refreshSubject.map{HomePageApi.glproductskus(pdid: $0)}
            .emeRequestApiForObj(ProductModel.self,activityIndicator: loading)
            .subscribe(onNext: {[unowned self] (result) in
                switch result {
                case .success(let data):
                    self.productItem.onNext(data)
                case .failure(let error):
                    self.error.onNext(error)
                }
            }).disposed(by: disposeBag)
        
        addcartsSubject
            .map{ osCarts in return AddCarApi.addcarts(osCarts: osCarts)}
            .emeRequestApiForRegJson()
            .subscribe(onNext: {[unowned self] (result) in
                switch result {
                case .success(let data):
                    self.addcartsSuccess.onNext(data)
                case .failure(let error):
                    self.error.onNext(error)
                }
            }).disposed(by: disposeBag)
        
        
        addusPdCollectionSubject
            .map{ item in return ProductApi.addusPdCollection(userid: item.userid, pdid: item.pdid)}
            .emeRequestApiForRegJson()
            .subscribe(onNext: {[unowned self] (result) in
                switch result {
                case .success(let data):
                    self.pdCollectionSuccess.onNext(data.stringValue)
                case .failure(let error):
                    self.error.onNext(error)
                }
            }).disposed(by: disposeBag)
        
        delusPdCollectionSubject
            .map{ cid in return ProductApi.delusPdCollection(cid: cid)}
            .emeRequestApiForRegJson()
            .subscribe(onNext: {[unowned self] (result) in
                switch result {
                case .success( _):
                    self.pdCollectionSuccess.onNext("")
                case .failure(let error):
                    self.error.onNext(error)
                }
            }).disposed(by: disposeBag)
        
        createOrderSubject
            .map{ order in return AddCarApi.createOrder(order)}
            .emeRequestApiForRegJson()
            .subscribe(onNext: {[unowned self] (result) in
                switch result {
                case .success(let rel):
                    self.createOrderSuccess.onNext(rel)
                case .failure(let error):
                    self.error.onNext(error)
                }
            }).disposed(by: disposeBag)
        
        
        
    }
    
}
